package com.gnerv.rabbitmq.agent.service;

import com.google.common.collect.Maps;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @author Gnerv LiGen
 */
@Slf4j
@Service
public class RabbitmqService {

    public boolean createExchange() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        // 设置端口号，默认为5672
        connectionFactory.setPort(5672);
        // 设置用户名，默认为guest
        connectionFactory.setUsername("admin");
        // 设置密码，默认为guest
        connectionFactory.setPassword("123456");
        // 设置虚拟主机，默认为 "/"
        connectionFactory.setVirtualHost("/");
        Connection connection = connectionFactory.createConnection();
        try (connection) {
            Channel channel = connection.createChannel(true);
            Map<String, Object> arguments = Maps.newHashMap();
            channel.exchangeDeclare("cccc", BuiltinExchangeType.DIRECT, true, false, false, arguments);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            connectionFactory.destroy();
        }
        return true;
    }


    public boolean deleteExchange() {
        return true;
    }

}
